<template>
  <div class="addsoftWorks-container-padding">
    <div v-if="!disabled" class="buttonList">
      <el-button type="primary" @click="handleSaveDraft">保存为草稿</el-button>
      <el-button type="primary" @click="submit">提交</el-button>
    </div>
    <el-form
      ref="baseInfoRef"
      :model="formline"
      :rules="rules"
      class="patent-form"
      label-width="auto"
    >
      <el-form-item label="版权号:" prop="copyrightCode">
        <el-input maxlength="30" v-model="formline.copyrightCode" />
      </el-form-item>
      <el-form-item label="软件名称:" prop="softwareName">
        <el-input maxlength="30" v-model="formline.softwareName" />
      </el-form-item>
      <el-form-item label="申请日期:" prop="applyDate">
        <el-date-picker
          v-model="formline.applyDate"
          type="date"
          placeholder="请选择"
          clearable
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="授权日期:" prop="authDate">
        <el-date-picker
          v-model="formline.authDate"
          type="date"
          placeholder="请选择"
          clearable
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="专利权人:" prop="owner">
        <el-input maxlength="30" v-model="formline.owner" />
      </el-form-item>
      <el-form-item label="完成单位:" prop="completeUnit">
        <el-input maxlength="30" v-model="formline.completeUnit" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
  import { reactive, withDefaults, defineProps, onMounted } from 'vue'
  import { useCloseTabNavigate } from '@/utils/useCloseTabNavigate'
  import { useRoute } from 'vue-router'
  import { addSoftware, getSoftwareInfo, updateSoftware } from '@/api/property'
  import { postDraftBoxCreate } from '@/api/project/drafts'

  const route = useRoute()
  const { closeTabNavigate } = useCloseTabNavigate()

  const formline = reactive({
    copyrightCode: '',
    softwareName: '',
    applyDate: '',
    authDate: '',
    owner: '',
    completeUnit: '',
  })
  const rules = reactive({})
  const props = withDefaults(
    defineProps<{
      disabled?: boolean
    }>(),
    {
      disabled: false,
    }
  )

  const softWorksId = ref('')

  onMounted(() => {
    const id = (route.query.id as string) ?? ''
    softWorksId.value = id
    if (id) {
      getSoftwareInfo({ id: id, getType: 2 }).then((res) => {
        for (const key in res.data) {
          formline[key] = res.data[key]
        }
      })
    }
    try {
      const inputList = document.querySelectorAll('input.el-input__inner')
      const maxLength = 30
      inputList.forEach((input: any) => {
        input.maxLength = maxLength
      })
    } catch (error) {}
  })
  // 草稿
  const handleSaveDraft = async () => {
    try {
      const params = {
        id: route.query.id || '',
        type: 'SOFTWARE',
        submitJson: JSON.stringify({
          ...formline,
          draftBoxId: route.query?.id || '',
        }),
      }
      const res: any = await postDraftBoxCreate(params)
      if (res && res.code === 0) {
        ElMessage.success('保存草稿成功')
        closeTabNavigate({ name: 'SoftWorks' })
      } else {
        ElMessage.error(`保存草稿失败: ${res.msg || '未知错误'}`)
      }
    } catch (e) {
      console.error('保存草稿失败', e)
    }
  }
  const submit = () => {
    if (softWorksId.value) {
      const params = {
        id: softWorksId.value,
        ...formline,
      }
      updateSoftware(params).then((res) => {
        ElMessage.success('修改成功')
        closeTabNavigate({ name: 'SoftWorks' })
      })
    } else {
      const params = {
        ...formline,
      }
      addSoftware(params).then((res) => {
        ElMessage.success('增加成功')
        closeTabNavigate({ name: 'SoftWorks' })
      })
    }
  }
</script>

<style scoped lang="scss">
  $margin-bottom: 20px;

  .buttonList {
    display: flex;
    justify-content: end;
    padding-bottom: $margin-bottom;
    border-bottom: 1px solid #edeff2;
    margin-bottom: $margin-bottom;
  }

  .patent-form {
    width: 60%;
    // margin: 0 auto;
  }
</style>
